# Create your views here.

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from market.marketData import MarketData
from market.authentification import authentificatePlayer, authentificateUser
from market.findNewRepRule2Ter import FindNewRepRule2TerHelper
from market.view import View
from market.mathView import view2vec
from market.models import UserProfile
from market.models import Asset,Player
from market.models import Strategy, AssetPercentageInStrategy, Fund
from market.mathView import indexOfAssetInList
from market.models import WebSiteComment
from market.jsonsForCharts import getComments
from market.controllerAdminView import getBetroomFromUser

from market.cometHelper import getMarketPlacePlayerStatus
from market.cometHelper import getChatRoomPlayerStatus

from django.utils import simplejson


###########################################################################################
# this controller is the one calling the view for the market
def marketView(request):

	# the user is here authentified - we get the matching player
	# if there is no "last visted betroom" sent the user tu pick up a bet room
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]
        
        betroom = player_list[0].dbPlayer.mainStrategy.betroom
        # if the betroom is being launched, redirect until is launched
        if betroom.status < 1 :
                return HttpResponseRedirect("/createBetroom/step2/")

        if betroom.roomType == 0:
                m = MarketData(betroom)
                return render_to_response(
                        'market/assetMarket.html',
                        {'market':m, 'player':player_list[0] },
                        context_instance=RequestContext(request) )
        else:
                return render_to_response(
                        'contractMarket/contractMarket.html',
                        {'player':player_list[0] },
                        context_instance=RequestContext(request) ) 



###########################################################################################
# this controller is the one calling the view for the strategies (market by probabilities only)
def strategiesView(request):

	# the user is here authentified - we get the matching player
	# if there is no "last visted betroom" sent the user tu pick up a bet room
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]
        
        
        betroom = player_list[0].dbPlayer.mainStrategy.betroom
        # if the betroom is being launched, redirect until is launched
        if betroom.status < 1 :
                return HttpResponseRedirect("/createBetroom/step2/")
        
        m = MarketData(betroom)
	return render_to_response(
                'market/marketStrategies.html',
                {'market':m, 'player':player_list[0]},
                context_instance=RequestContext(request) )

###########################################################################################
# this controller is the one calling the view for to define a nex contract in the market by contract onlt
def newContractView(request):

	# the user is here authentified - we get the matching player
	# if there is no "last visted betroom" sent the user tu pick up a bet room
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]
        
        
        betroom = player_list[0].dbPlayer.mainStrategy.betroom
        # if the betroom is being launched, redirect until is launched
        if betroom.status < 1 :
                return HttpResponseRedirect("/createBetroom/step2/")
        
	return render_to_response(
                'contractMarket/createNewContract.html',
                {'player':player_list[0]},
                context_instance=RequestContext(request) )

###########################################################################################
# this controller is the one calling the view for the portfolio
def portfolioView(request):


	# the user is here authentified - we get the matching player
	# if there is no "last visted betroom" sent the user tu pick up a bet room
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]

        betroom = player_list[0].dbPlayer.mainStrategy.betroom
        # if the betroom is being launched, redirect until is launched
        if betroom.status < 1 :
                return HttpResponseRedirect("/createBetroom/step2/")
        
        if betroom.roomType == 0:
                htmlPath = 'market/marketPortfolio.html'
        else:
                htmlPath = 'contractMarket/contractPortfolio.html'
                
	return render_to_response( htmlPath, {'player':player_list[0] }, context_instance=RequestContext(request) )


             
###########################################################################################
# this controller is the one calling the view for the help
def derivativesView(request):

	# the user is here authentified - we get the matching player
	# if there is no "last visted betroom" sent the user tu pick up a bet room
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]

        betroom = player_list[0].dbPlayer.mainStrategy.betroom
        # if the betroom is being launched, redirect until is launched
        if betroom.status < 1 :
                return HttpResponseRedirect("/createBetroom/step2/")

        if betroom.roomType == 0:
                folderName = 'market/'
        else:
                folderName = 'contractMarket/'
                
	return render_to_response(
                folderName + 'marketDerivatives.html',
                {'player':player_list[0] },
                context_instance=RequestContext(request) )


